# -*-coding:utf-8-*-

import os
import re

image_name = 'dolphi_nav'
version ='1.0'
image_path=f'registry.cn-hangzhou.aliyuncs.com/dolphi/{image_name}:{version}'
docker_build=f'docker build -t {image_name}:{version} .'
docker_login = 'docker login --username=179049114@qq.com registry.cn-hangzhou.aliyuncs.com'



def  get_Image_Id():
    docker_ps = 'docker images '
    a = os.popen(docker_ps)
    subject = a.read()
    print('-----------\n',subject)
    match = re.search(f"{image_name} +{version}(.*)", subject)
    if match:
        subject = match.group(1)
        match = re.search(r"(\w{12})", subject)
        if match:
            id = match.group(1)
            return id
    a.close()  # 打印后还需将对象关闭
    return ''

def run_docker_build():
    a = os.popen(docker_build)
    print(a.read())
    a.close()
#run_docker_build()
ImageId = get_Image_Id()

docker_tag = f'docker tag {ImageId} {image_path}'
docker_push=f'docker push {image_path}'
docker_pull=f'docker pull {image_path}'

docker_run1 = f'docker run -d -p 8089:8089 --name {image_name}_1   --restart=always  -e DATABASE_USER=root -e DATABASE_PASSWORD=dolphi123 -e DATABASE_HOST=127.0.0.1   {image_name}:{version}'
docker_run2 = f'docker run -d -p 8089:8089 --name {image_name}_1   --restart=always   -e DATABASE_USER=root -e DATABASE_PASSWORD=dolphi123 -e DATABASE_HOST=127.0.0.1   {image_path}'
print(f'{docker_login}\n{docker_tag}\n{docker_push}\n\n\n{docker_login}\n{docker_pull}\n\n{docker_run1}\n\n{docker_run2}')
